require "rexml/document"
include REXML

class IndexResponse
  
  attr_accessor :request_id, :image_url, :image_id, :entity_id, :entity_name, :result_code, :result_description, :roi_descriptors

  def initialize(request_id = nil, image_url = nil)

    
  end
  
   def self.element_value(node, expression)
    
    elements = node.elements[expression]
    
    if (elements != nil) 
      return elements[0].to_s()
    else
      return nil
    end
    
  end
  
  def self.fromXml(doc) 
   
    r = IndexResponse.new()
    r.request_id = element_value(doc, "index-response/request-id")
    r.image_url = element_value(doc, "index-response/image-url")
    r.image_id = element_value(doc, "index-response/image-id")
    r.entity_id = element_value(doc, "index-response/entity-id")
    r.entity_name = element_value(doc, "index-response/entity-name")
    r.result_code = element_value(doc, "index-response/result-code")
    r.result_description = element_value(doc, "index-response/result-description")
    
    r.roi_descriptors = []
    
    i = 0
    
    doc.elements.each("index-response/roi-descriptor") do |roiNode|
      roi = RoiDescriptor.new();
      roi.roi_id = element_value(roiNode, "roi-id")
      roi.bounding_box = element_value(roiNode, "bounding-box")
      roi.type_code = element_value(roiNode, "type-code")
      roi.gender = element_value(roiNode, "gender")
      roi.gender_score = element_value(roiNode, "gender_score")
      
      r.roi_descriptors[i] = roi
      i = i + 1
      
    end
    
 
    return r
    
  end
  
  class RoiDescriptor
    attr_accessor :roi_id, :bounding_box, :type_code, :gender, :gender_score 

    def initialize()
      
    end
    
    
    
  end
  
end